package my.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import af.spring.AfRestData;
import af.spring.AfRestError;
import my.db.AssessPublishtable;
import my.db.AssessStatistics;
import my.db.AssessTable;
import my.db.Student;
import my.tool.MybatisApi;

@Controller
public class AssessController {

	@GetMapping("assess")
	public String Assess(Model model,HttpServletRequest request) {
		
		List<AssessTable> QuestionList = MybatisApi.selectList("zhy.sesAssess.getQuestion", AssessTable.class);
		
		List<Map<String, Object>> RespondentsList = MybatisApi.selectList("zhy.sesAssess.getRespondents",0);
		
		List<AssessStatistics> StatisticsList = MybatisApi.selectList("zhy.sesAssess.getStatistics", AssessStatistics.class);
		model.addAttribute("QuestionList", QuestionList);
		model.addAttribute("RespondentsList", RespondentsList);
		model.addAttribute("StatisticsList", StatisticsList);
		model.addAttribute("datas", IndexController.SimpleJS(request));
		return "Assess_Manager";
	}
	
	@GetMapping("viewmore")
	public String ViewMore(Model model,HttpServletRequest request,int AS_id,String respondents) {
		
		List<Integer> WhereTeacherList = MybatisApi.selectList("zhy.sesAssess.getWhereTeacher", AS_id);
		List<Double> PointList = new ArrayList<Double>();
		List<Map<String, Object>> TeacherMoreList = new ArrayList<Map<String,Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("AS_id", AS_id);
  		for (Integer teacherid : WhereTeacherList) {
			map.put("Appraisee", teacherid);
			PointList.add((Double) MybatisApi.select("zhy.sesAssess.getPoint",map));
			TeacherMoreList.add((Map<String, Object>) MybatisApi.select("zhy.sesAssess.getWhereTeacherMore", map)); 
		}
  		for (int i =0;i<TeacherMoreList.size();i++) {
			TeacherMoreList.get(i).put("Point", PointList.get(i));
		}
  		
  		model.addAttribute("TeacherMoreList", TeacherMoreList);
  		model.addAttribute("respondents",respondents);
		model.addAttribute("datas", IndexController.SimpleJS(request));
		return "ViewMore";
	}
	
	//新增一个问题
	@PostMapping("assess_saveQuestion.do")
	public Object saveQuestion(@RequestBody JSONObject jreq) {
		
		AssessTable Atable = new AssessTable();
		Atable.setQuestion(jreq.getString("question"));
		Atable.setRespondents(jreq.getString("respondents"));
		MybatisApi.insert("zhy.sesAssess.saveQuestion", Atable);
		return new AfRestData("");
	}
	
	//新增一个发布
		@PostMapping("assess_savePublish.do")
		public Object savePublish(@RequestBody JSONObject jreq) {
			
			AssessStatistics statistics = jreq.getObject("statistics", AssessStatistics.class);
			List<AssessStatistics> repeat = MybatisApi.selectList("zhy.sesAssess.getRepeat", statistics);
			if(repeat.size()>0) {
				return new AfRestError("重复的发布");
			}
			MybatisApi.insert("zhy.sesAssess.saveStatistics", statistics);
			
			JSONArray publish = jreq.getJSONArray("publish");
			for(int i=0;i<publish.size();i++) {
				AssessPublishtable p = publish.getObject(i, AssessPublishtable.class);
				p.setAS_id(statistics.getId());
				MybatisApi.insert("zhy.sesAssess.savePublish", p);
			}
			
			return new AfRestData("");
		}
	
	//删除问题
	@PostMapping("assess_deleteQuestion.do")
	public Object deleteQuestion(@RequestBody JSONArray jreqs) {
		
		for(int i=0;i<jreqs.size();i++) {
			MybatisApi.delete("zhy.sesAssess.deleteQuestion", jreqs.get(i));
		}
		return new AfRestData("");
	}
	
	//删除统计表数据
		@PostMapping("assess_deleteStatisticsn.do")
		public Object deleteStatistics(@RequestBody JSONArray jreqs) {
			
			for(int i=0;i<jreqs.size();i++) {
				MybatisApi.delete("zhy.sesAssess.deleteStatistics", jreqs.get(i));
			}
			return new AfRestData("");
		}
	
	//修改一个问题
		@PostMapping("assess_updateQuestion.do")
		public Object updateQuestion(@RequestBody JSONArray jreqs) {
			
			for(int i=0;i<jreqs.size();i++) {
				MybatisApi.update("zhy.sesAssess.updateQuestion",jreqs.getObject(i, AssessTable.class));
			}
			return new AfRestData("");
		}
		
		//修改发布状态
		@PostMapping("assess_updatePublish.do")
		public Object updatePublish(@RequestBody JSONArray jreqs) {
			
			for(int i=0;i<jreqs.size();i++) {
				MybatisApi.update("zhy.sesAssess.updatePublish",jreqs.getInteger(i));
			}
			return new AfRestData("");
		}
		
		//查看此次已发布的问题
		@PostMapping("assess_getThisPublish.do")
		public	Object getThisPublish(@RequestBody JSONObject jreq) {
			List<AssessPublishtable> ThisQuestionList = MybatisApi.selectList("zhy.sesAssess.getPublishedQuestion", jreq.getInteger("id"));
			return new AfRestData(ThisQuestionList);
		}
		
}
